package com.evon.yardmanagement.ui.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;



import com.evon.yardmanagement.model.Action;
import com.evon.yardmanagement.model.Role;
import com.evon.yardmanagement.model.User;
import com.evon.yardmanagement.model.User_Role;
import com.evon.yardmanagement.security.YardSecurityManager;
import com.evon.yardmanagement.service.RoleManagementService;
import com.evon.yardmanagement.service.UserManagementService;
import com.evon.yardmanagement.transformer.UserTransformer;
import com.evon.yardmanagement.ui.dto.RoleDTO;
import com.evon.yardmanagement.ui.dto.SectionDTO;
import com.evon.yardmanagement.ui.dto.transaction.UserDTO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * Action class for handling the user management actions.
 * @author vgaurav
 *
 */
public class UserManagementAction extends ActionSupport {
	
	private List<User> listUser = new ArrayList<User>();
	private List<String> userEmail = new ArrayList<String>();
	private List<String> role = new ArrayList<String>();
	private UserDTO userDto;
	private int selectedUserId;
	private List<User> listSpecificUser = new ArrayList<User>();
	List<UserDTO> userDTOList = new ArrayList<UserDTO>();
	List<Integer> selectedAllowedRole = new ArrayList<Integer>();
	
	private List<RoleDTO> listAllRoles;
	
	
	
	public String execute() throws Exception {
		
		
		return SUCCESS;
		}
	public String getUsers(){
		
		//listUser = new UserManagementService().getAllUsers();
		listUser = new UserManagementService().getUserForCompany(YardSecurityManager.getCurrentVendor().getId());
		userDto = new UserDTO();
		for (User user : listUser) {
			userEmail.add(user.getUserName());
			userDto.setUserName(user.getUserName());
			userDto.setUserFirstName(user.getFirstName());
			userDto.setUserLastName(user.getLastName());
			userDto.setUserMiddleName(user.getMiddleName());
		}
		
	
		
		return SUCCESS;
	}
	public String userDetails(){
		System.out.println("id"+selectedUserId);
		listSpecificUser = new UserManagementService().getSpecificUsers(selectedUserId);
		
		if (listSpecificUser != null ) {
			for (User user : listSpecificUser) {
			Set<Role> specificRoles =	user.getRoles();
			             
			for (Role role : specificRoles) {
				
				System.out.println("-------------------1"+role.getId());
				selectedAllowedRole.add(role.getId());
			}
		}
		}
		userDTOList = UserTransformer.transformToDTO(listSpecificUser);
		
		listAllRoles = RoleManagementService.getAllRoles(YardSecurityManager.getCurrentVendor().getId());
		return SUCCESS;
	}
	
	public String save(){
		
		List<User_Role> selectedRoles = new ArrayList<User_Role>();
		if(selectedAllowedRole != null || !selectedAllowedRole.isEmpty()){
			for(Integer roleId : selectedAllowedRole){
				User_Role role = new User_Role();
				role.setId(roleId);
				selectedRoles.add(role);
			}
		}
		
		UserManagementService userManagementService = new UserManagementService();
		userManagementService.save(selectedRoles);
		
		return SUCCESS;
	}

	public List<User> getListUser() {
		return listUser;
	}

	public void setListUser(List<User> listUser) {
		this.listUser = listUser;
	}

	public List<String> getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(List<String> userEmail) {
		this.userEmail = userEmail;
	}

	public UserDTO getUserDto() {
		return userDto;
	}

	public void setUserDto(UserDTO userDto) {
		this.userDto = userDto;
	}
	public int getSelectedUserId() {
		return selectedUserId;
	}
	public void setSelectedUserId(int selectedUserId) {
		this.selectedUserId = selectedUserId;
	}
	public List<User> getListSpecificUser() {
		return listSpecificUser;
	}
	public void setListSpecificUser(List<User> listSpecificUser) {
		this.listSpecificUser = listSpecificUser;
	}
	public List<String> getRole() {
		return role;
	}
	public void setRole(List<String> role) {
		this.role = role;
	}
	public List<UserDTO> getUserDTOList() {
		return userDTOList;
	}
	public void setUserDTOList(List<UserDTO> userDTOList) {
		this.userDTOList = userDTOList;
	}
	public List<Integer> getSelectedAllowedRole() {
		return selectedAllowedRole;
	}
	public void setSelectedAllowedRole(List<Integer> selectedAllowedRole) {
		this.selectedAllowedRole = selectedAllowedRole;
	}
	public List<RoleDTO> getListAllRoles() {
		return listAllRoles;
	}
	public void setListAllRoles(List<RoleDTO> listAllRoles) {
		this.listAllRoles = listAllRoles;
	}
	
}
